package syn;

// 不安全的取钱
public class Money {
    public  static void main(String[] args) {
        Account account = new Account(1000,"建设银行");
        bank bank1 = new bank(account,800,"我");
        bank bank2 = new bank(account,700,"傻逼");
        bank1.start();
        bank2.start();
    }

}


// 银行类
class bank extends Thread{
      final Account account; // 银行卡
    int getmoney; // 取走的钱

    public bank(Account account, int getmoney, String name) {
        super(name);
        this.account = account;
        this.getmoney = getmoney;
    }

    private int vacancies; // 手里的钱

    @Override
    public  void run() {
        synchronized (account){
               // 判断还有没有钱
               if (account.money - getmoney<=0){
                   System.out.println(this.getName()+"钱不够了");
                   return;
               }
               // 取钱
               try {
                   Thread.sleep(100);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               account.money = account.money - getmoney;
               vacancies = vacancies + getmoney;
               System.out.println(this.getName()+"取走了"+getmoney+"还剩下"+account.money);
               System.out.println(this.getName()+"有"+vacancies);
               System.out.println(account.money);
           }

        }

}

// 账户类
class  Account{
    int money;
    String name;

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
